﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TexasHoldem.RankedHands;

namespace TexasHoldem
{
    public class HandRanker: IHandRanker
    {
        private IEnumerable<Func<Hand, RankedHand>> _rankedHandFactoryChain;

        public HandRanker()
        {
            _rankedHandFactoryChain = new Func<Hand, RankedHand>[] { 
                StraightFlushHand.TryCreateFrom, PokerHand.TryCreateFrom, FullHouseHand.TryCreateFrom,
                FlushHand.TryCreateFrom, StraightHand.TryCreateFrom, TripsHand.TryCreateFrom,
                TwoPairHand.TryCreateFrom, PairHand.TryCreateFrom, HighCardHand.TryCreateFrom
            };
        }

        public RankedHand RankHand(Hand hand)
        {
            return _rankedHandFactoryChain.Select(factory => factory(hand)).First(rankedHand => rankedHand != null);
        }
    }
}
